package com.HouseManage.utils;

import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Scanner;

public class UserUtil {
    boolean state = false;
    private ResourceBundle bundle =  ResourceBundle.getBundle("resource/db");
    private String url = bundle.getString("url");
    private String user = bundle.getString("user") ;
    private String password = bundle.getString("password");
    private String driver;
    Connection con;
    Statement stt;

    public UserUtil() {
        try{
            driver = bundle.getString("driver");
            Class.forName(driver);
        }catch (ClassNotFoundException e){
            System.out.println(e);
        }
    }


//返回HashMap用于验证登录

    public boolean checkState(String user, String password){
        try{
            con = DriverManager.getConnection(url, "root", "131131as");
            stt = con.createStatement();
            String insert = "select * from user where user = '" + user + "' and password = '" + password + "'";
            ResultSet rs = stt.executeQuery(insert);
            if (rs.next()) {
                state = true;
            }
            con.close();
            stt.close();
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return state;
    }


//检查是否登陆
    public void addMessage(String name,String password){
        try{
            con = DriverManager.getConnection(url, "root", "131131as");
            stt = con.createStatement();
            String insert = "insert into user(user,password,real_name) value ('"+name+"',"+password+",'一般用户')";
            stt.executeUpdate(insert);
            con.close();
            stt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
//注册
public void delMessage(String name,String password){
    try{
        con = DriverManager.getConnection(url, "root", "131131as");
        stt = con.createStatement();
        String insert = "delete from user where user = '"+name+"' and password = '"+password+"'";
        stt.executeUpdate(insert);
        con.close();
        stt.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}
//注销
    public Map<String,String> getMessage(String user,String password){
        String loguser = user;
        String logpassword = password;
        Map<String,String> map = new HashMap<>();
        map.put("user",loguser);
        map.put("password",logpassword);
        return map;
    }
}
